#!/bin/bash
# 安装 systemd 服务并构建二进制
set -e
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
BIN="$ROOT/cmd/xfio-controller/xfio-controller"
SERVICE_SRC="$ROOT/cmd/xfio-controller/xfio-controller.service"
SERVICE_DST="/etc/systemd/system/xfio-controller.service"

echo "构建 xfio-controller 二进制..."
cd "$ROOT"
go build -o "$BIN" ./cmd/xfio-controller

if [ -f "$SERVICE_SRC" ]; then
    echo "复制 systemd 服务单元到 $SERVICE_DST"
    sudo cp "$SERVICE_SRC" "$SERVICE_DST"
    sudo systemctl daemon-reload
    sudo systemctl enable xfio-controller
    echo "启用并启动服务..."
    sudo systemctl start xfio-controller
    sudo systemctl status xfio-controller --no-pager
else
    echo "未找到服务单元文件 $SERVICE_SRC"
fi
